//---------------------------------------------------------------------
//  This file is part of the Background Motion solution.
// 
//  Copyright (C) Mindscape (TM).  All rights reserved.
//  http://www.mindscape.co.nz
// 
//  THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//---------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;

using Microsoft.Practices.ObjectBuilder;
using Mindscape.BackgroundMotion.Model;
using Mindscape.BackgroundMotion.Website.Views;

namespace Mindscape.BackgroundMotion.Website.Infrastructure
{
  /// <summary>
  /// The base class for all the view pages in the Background Motion system
  /// </summary>
  /// <remarks>
  /// PageBase helds wire up the relationship between a View, a Presenter and an Application Controller as a triplet that will be used for actual runtime execution of a
  /// selected view when it is browsed.
  /// Presenter and Controller are largely for convention so we can refer to .Controller and .Presenter by strong typing rather than having explicit casting required
  /// </remarks>
  /// <typeparam name="TPresenter">The presenter being used</typeparam>
  /// <typeparam name="TView">The view being implemented</typeparam>
  /// <typeparam name="TController">The controller being used</typeparam>
  public class PageBase<TPresenter, TView, TController> : Page
    where TPresenter : PresenterBase<TView, TController>
    where TView : IView
  {
    private TPresenter _presenter;

    /// <summary>
    /// Raises the <see cref="E:System.Web.UI.Control.Load"></see> event.
    /// </summary>
    /// <param name="e">The <see cref="T:System.EventArgs"></see> object that contains the event data.</param>
    protected override void OnLoad(EventArgs e)
    {
      base.OnLoad(e);

      if (!IsPostBack)
      {
        _presenter.OnViewInitialized();
      }
      
      _presenter.OnViewLoaded();
    }

    /// <summary>
    /// Gets or sets the presenter to be associated with the view
    /// </summary>
    /// <value>The presenter.</value>
    [CreateNew]
    public TPresenter Presenter
    {
      get { return _presenter; }
      set
      {
        _presenter = value;
        _presenter.View = (TView)((IView)this);
      }
    }

    /// <summary>
    /// Helper function to bind to a drop down list.
    /// </summary>
    /// <param name="dropDownList">The drop down list.</param>
    /// <param name="dataValueField">The data value field.</param>
    /// <param name="dataTextField">The data text field.</param>
    /// <param name="list">The list.</param>
    protected static void BindDropDownList(DropDownList dropDownList, string dataValueField, string dataTextField, IList<ContentType> list)
    {
      dropDownList.DataSource = list;
      dropDownList.DataTextField = dataTextField;
      dropDownList.DataValueField = dataValueField;
      dropDownList.DataBind();
    }
  }
}